using System.Collections.Generic;
using System.Data.SqlClient;
using DapperExtensions;

namespace ExaminationSys.DataAccess.Repository
{
    public abstract class Repository<T> : IRepository<T>
        where T : class
    {
        protected DapperExtensions.DapperExtensions.IDapperExtensionsImpl _objectSet;
        private SqlConnection db;
        public Repository(SqlConnection conn)
        {
            _objectSet = DapperExtensions.DapperExtensions.Instance;
            db = conn;
        }

        public virtual DapperExtensions.DapperExtensions.IDapperExtensionsImpl ObjectSet
        {
            get
            {
                return _objectSet;
            }
        }

        #region IRepository<T> Members


        public abstract T GetById(dynamic id);


        public IEnumerable<T> GetList()
        {
            return _objectSet.GetList<T>(db, null, null, null, null, true);
        }

        public IEnumerable<T> GetList(IPredicate predicate)
        {
            return _objectSet.GetList<T>(db, predicate, null, null, null, true);
        }

        public void Insert(T entity)
        {
            _objectSet.Insert(db, entity, null, null);
        }


        public bool Update(T entity)
        {
            return _objectSet.Update(db, entity, null, null);
        }
        public void Delete(T entity)
        {
            _objectSet.Delete(db, entity, null, null);
        }

        public bool Delete(IPredicate predicate)
        {
            return _objectSet.Delete(db, predicate, null, null);

        }

        #endregion


        public dynamic Insert(T entity, int? commandTimeout)
        {
            return _objectSet.Insert(db, entity, null, commandTimeout);
        }
    }
}